package fwhmc.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.core.Ordered;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * 类描述:  跨域配置
 *
 * @author 王雷
 * @version 1.0.0
 * @date 2022/4/9 0009 上午 10:20
 */
@Slf4j
//@Configuration
public class WebCorsConfig {
    /**
     * 功能描述：构建跨域处理
     * @date : 2022/4/9 0009 上午 10:21
     */
    protected CorsConfiguration buildCorsConfiguration(){
        final CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.setAllowCredentials(true);

//        corsConfiguration.setAllowedOrigins(Collections.singletonList("http://sltappadmintest.blg1999.cn/"));
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
//        corsConfiguration.setAllowedHeaders(Arrays.asList("Authorization",  "Access-Control-Allow-Origin", "Access-Control-Allow-Credentials", "Content-Type", "Access-Control-Request-Method", "Access-Control-Request-Headers"));
//        corsConfiguration.setAllowedMethods(Arrays.asList("GET","POST","HEAD","OPTIONS","PUT"));
        corsConfiguration.addAllowedMethod("*");
        return corsConfiguration;
    }

    /**
     * 功能描述：注入跨域处理拦截器
     * @date : 2022/4/9 0009 上午 10:26
     */
    @Bean
    public FilterRegistrationBean<CorsFilter> corsFilter(){
        log.error("=====================================HTTP-WEB——跨域配置执行=====================================");
        final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        //配置跨域路径
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", buildCorsConfiguration());

        final FilterRegistrationBean<CorsFilter> corsFilterFilterRegistrationBean
                = new FilterRegistrationBean<>(new CorsFilter(urlBasedCorsConfigurationSource));

        corsFilterFilterRegistrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return corsFilterFilterRegistrationBean;
    }
}
